package com.luckymore.ydd.app.funcs.login;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.base.cache.ACacheUtil;
import com.base.control.ContextControl;
import com.base.control.MFragmentSupport;
import com.base.control.TaskWithExceptionHandler;
import com.base.control.TextParamCheck;
import com.base.model.Bean2Json;
import com.base.util.Helper;
import com.base.util.StringUtil;
import com.base.view.holder.ActionBarCommons;
import com.luckymore.ydd.app.R;
import com.luckymore.ydd.app.cache.CacheUtil;
import com.luckymore.ydd.app.funcs.mainfuncs.MainFuncsActivity;
import com.luckymore.ydd.app.model.MAppUser;
import com.luckymore.ydd.app.model.MSystem;
import com.luckymore.ydd.app.net.ApiClientFactory;
import com.luckymore.ydd.app.util.ActionBarSubControl;
import com.luckymore.ydd.app.util.AppConfig;
import com.luckymore.ydd.app.util.MErrors;

/**
 * Created by yjh on 15-3-12.
 */
public class AccountSetFragment extends MFragmentSupport {
    private EditText passwdEdit;
    private EditText nicknameEdit;
    private RadioGroup sexRg;
    private Button finishBtn;

    @Override
    protected void create(Bundle savedInstanceState) {
        setContentView(R.layout.fragment_account_info);

        passwdEdit = findViewById(R.id.account_password);
        nicknameEdit = findViewById(R.id.account_nickname);
        sexRg = findViewById(R.id.account_set_sex);
        finishBtn = findViewById(R.id.account_set_finish);

        MAppUser.MUser mUser = ACacheUtil.getData(getMactivity(), CacheUtil.USER_INFO_CACHE_KEY);
        if(mUser != null) {
            nicknameEdit.setText(StringUtil.emptyIfBlank(mUser.getNickname()));
        }

        finishBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new TaskWithExceptionHandler() {
                    @Override
                    public void onCheck() throws Exception {
                        TextParamCheck
                                .check(passwdEdit.getText().toString(), MErrors.PASSWD_EMPTY, MErrors.PASSWD_FORMAT_ERROR, TextParamCheck.PASSWORD_CHECKER);

                        TextParamCheck
                                .check(nicknameEdit.getText().toString(), MErrors.NICKNAME_EMPTY, MErrors.NICKNAME_FORMAT_ERROR, TextParamCheck.EMPTY_CHECKER);
                        int sex = sexRg.getCheckedRadioButtonId() == R.id.male ? 0 : 1;
                        AppConfig.PASSWORD = passwdEdit.getText().toString();
                        ApiClientFactory.updateUserInfo(getMactivity(), AccountSetFragment.this, "updateCallBack", AppConfig.PASSWORD, nicknameEdit.getText().toString(), sex);
                    }
                }.work(getActivity());
            }
        });
    }

    @Override
    public void setActionBar(ActionBar actionBar, Context context) {
        super.setActionBar(actionBar, context);

        ActionBarCommons abc = ActionBarSubControl.setActionBarCommons(getMactivity());
        abc.getTitleTv().setText("账号设置");
        abc.getLeftBtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //返回上一级
                getFragmentManager().popBackStack();
            }
        });

    }

    public void updateCallBack(Bean2Json data) {
        MSystem.MRet ret = (MSystem.MRet)data;
        if(ret != null) {
            if(ret.getCode() == 1) {
                ContextControl.startContext(getActivity(), MainFuncsActivity.class, true);
            }
            Helper.toast(getActivity(), ret.getMsg());
        }
    }
}
